/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bean.timer;

import com.bean.timer.interfaces.AbstractCompositeTimer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import javax.swing.JPanel;

/**
 *
 * @author sammael
 */
public class CompositeTimer extends AbstractCompositeTimer implements Serializable{
    
    
    private PropertyChangeSupport propertySupport;
    private boolean started = false;
    
    public CompositeTimer() {
        propertySupport = new PropertyChangeSupport(this);
        this.setLayout(new GridBagLayout());
        GridBagConstraints gbconst = new GridBagConstraints();
        gbconst.insets = new Insets(10, 10, 10, 10);
        gbconst.gridx = 0;
        gbconst.gridy = 0;

        buttonStartStop.setText(getButtonStartText());
        buttonReset.setText("Reset");
        
        panelButton = new JPanel(new BorderLayout());
        panelButton.add(buttonStartStop,BorderLayout.NORTH);
        panelButton.add(buttonReset, BorderLayout.SOUTH);
        
        panelButton.setBackground(Color.decode("0x78c7d4"));
        JPanel panel2 = new JPanel(new BorderLayout());
        
        panel2.add(timer, BorderLayout.CENTER);
        panel2.add(panelButton,BorderLayout.EAST);
        this.add(panel2, gbconst);
        buttonStartStop.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
               if(!started){
                startTimer();
                started = true;
                buttonStartStop.setText(getButtonStopText());
               }else{
                stopTimer();
                started= false;
                buttonStartStop.setText(getButtonStartText());
               }
            }
        });
        
        buttonReset.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                timer.resetTimer();
            }
        });
    }

    @Override
    protected void propertyChange(String property, long oldval, long newval) {
        propertySupport.firePropertyChange(property, oldval, newval);
    }

    @Override
    protected void propertyChange(String property, int oldval, int newVal) {
        propertySupport.firePropertyChange(property, oldval, newVal);
    }

    @Override
    protected void propertyChange(String property, Object oldval, Object newVal) {
        propertySupport.firePropertyChange(property, oldval, newVal);
    }

    @Override
    protected void propertyChange(String property, boolean oldval, boolean newVal) {
        propertySupport.firePropertyChange(property, oldval, newVal);
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        if(this.isGradientBackground()){
            GradientPaint p;
            p = new GradientPaint(0, 0, getFirstGradientColor(), 0, getHeight(),getSecondGradientColor());
            g2.setPaint(p);
        }
	else
            g2.setColor(getFirstGradientColor());
        
	g2.fillRect(0, 0, getWidth(), getHeight());
    }
    
}
